<?php

if(!isset($_SESSION["id_membre"])) {
session_start();
}


if( file_exists("../../lib/mysql_connect.php") )
  require("../../lib/mysql_connect.php");
else
  require("../lib/mysql_connect.php");

/********************************************************
 * Traitement PHP :                                     *
 * Ensemble des fonctions utilisée dans le traitement   *
 * PHP                                                  *
 * *****************************************************/

function get_detail_seance_bilan($id_seance)
{
  $requete = "SELECT nom, charge, serie, repetition FROM atelier_bilan_seance, atelier, exercice_bilan_seance WHERE
    id_seance = '".$id_seance."'
    AND atelier_bilan_seance.id_atelier_bilan_seance  = exercice_bilan_seance.id_atelier_bilan_seance
    AND atelier.id_atelier = atelier_bilan_seance.id_atelier 
    ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $nb_projet = mysql_num_rows($requete_result);
    if( $nb_projet )
    {
      while($row = mysql_fetch_array($requete_result)) {

        echo "
          <tr>
          <td>".$row['nom']."</td><td>".$row['charge']."</td><td>".$row['serie']."</td><td>".$row['repetition']."</td>
          </tr>

          ";
      }
    }
    else
    {
      echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td></tr>";
    }
  }
  else
  {
    echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td></tr>";
  }
}

function get_detail_seance($id_seance)
{
  $requete = "SELECT nom, charge, serie, repetition FROM atelier_projet_seance, atelier, exercice_projet_seance WHERE
    id_seance = '".$id_seance."'
    AND atelier_projet_seance.id_atelier_projet_seance  = exercice_projet_seance.id_atelier_projet_seance
    AND atelier.id_atelier = atelier_projet_seance.id_atelier 
    ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $nb_projet = mysql_num_rows($requete_result);
    if( $nb_projet )
    {
      while($row = mysql_fetch_array($requete_result)) {

        echo "
          <tr>
          <td>".$row['nom']."</td><td>".$row['charge']."</td><td>".$row['serie']."</td><td>".$row['repetition']."</td>
          </tr>

          ";
      }
    }
    else
    {
      echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td></tr>";
    }
  }
  else
  {
    echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td></tr>";
  }
}

function bilan_seance($id_seance)
{
  $requete = "SELECT ressenti,commentaire FROM bilan_seance WHERE
    id_seance = '".$id_seance."' ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $nb_projet = mysql_num_rows($requete_result);
    if( $nb_projet )
    {
      $row = mysql_fetch_array($requete_result);
      $res = array( "ressenti" => $row['ressenti'], "commentaire" => $row['commentaire']);
      return $res;

    }else
      return "Bilan de séance non effectué";
  }
  else
    return "Bilan de séance non effectué";

}

function get_seance_par_date($date)
{
  $requete = "SELECT id_seance, seance.id_eleve as id_eleve FROM seance,eleve WHERE
    date = '".$date."' 
    AND eleve.id_eleve = seance.id_eleve
    AND id_enseignant = '".$_SESSION['id_membre']."'
    ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $nb_projet = mysql_num_rows($requete_result);
    if( $nb_projet )
    {
      while($row = mysql_fetch_array($requete_result)) {

        echo "<tr>
          <td>".get_name($row['id_eleve'],"eleve");

        $projet = get_projet($row['id_eleve']);

        if(count($projet) <= 1)
        {
          $desc = "-";
          $proj = "-";
        }else{
          $desc = $projet['desc'];
          $proj = $projet['mobile'];
        }

        echo "<td>".$proj."</td><td>".$desc."</td><td><input type='button' name='detail' value='V' id='".$row['id_seance']."' onClick='javascript:seance_detail(".$row['id_seance'].")'/></td>
          </tr>";
      }
    }
    else
    {
      echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td> </tr>";
    }
  }
  else
  {
    echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td> </tr>";
  }
}

function get_seance_complet()
{
  $requete = "SELECT date, id_seance, seance.id_eleve as id_eleve FROM seance,eleve WHERE
    eleve.id_eleve = seance.id_eleve
    AND id_enseignant = '".$_SESSION['id_membre']."'
    ORDER BY date
    ";
  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $nb_projet = mysql_num_rows($requete_result);
    if( $nb_projet )
    {
      while($row = mysql_fetch_array($requete_result)) {

        echo "
          <td>".$row['date']."</td>
          <td>".get_name($row['id_eleve'],"eleve");

        $projet = get_projet($row['id_eleve']);

        if(count($projet) <= 1)
        {
          $desc = "-";
          $proj = "-";
        }else{
          $desc = $projet['desc'];
          $proj = $projet['mobile'];
        }

        echo "<td>".$proj."</td><td>".$desc."</td><td><input type='button' name='detail' value='V' id='".$row['id_seance']."' onClick='javascript:seance_detail(".$row['id_seance'].")'/></td>
          </tr>";
      }
    }
    else
    {
      echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td></tr>";
    }  
  }
  else
  {
    echo "<tr> <td>-</td> <td>-</td> <td>-</td> <td>-</td> <td>-</td></tr>";
  }
}


function get_seance($id_eleve)
{
  $requete = "SELECT id_seance,date FROM seance WHERE
    id_eleve = '".$id_eleve."' ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $nb_projet = mysql_num_rows($requete_result);
    if( $nb_projet )
    {
      while($row = mysql_fetch_array($requete_result)) {

        echo "<tr>
          <td>".$row['date']."</td><td><input type='button' name='detail' value='V' id='".$row['id_seance']."' onClick='javascript:seance_detail(".$row['id_seance'].")'/></td>
          </tr>";
      }
    }else
      {
        echo "<tr><td>-</td><td>-</td></tr>";
      }
  }
  else
  {
    echo "<tr><td>-</td><td>-</td></tr>";
  }

}

function get_eleve()
{
  $requete_eleve = "SELECT id_eleve, nom, prenom FROM eleve WHERE
    id_enseignant = '".$_SESSION['id_membre']."' ";

  $requete_eleve_result = mysql_query($requete_eleve);

  if( $requete_eleve_result != FALSE )
  {
    $nb_eleve = mysql_num_rows($requete_eleve_result);
    if( $nb_eleve )
    {
      while($row = mysql_fetch_array($requete_eleve_result)) {
        echo '<OPTION VALUE='.$row['id_eleve'].'>'.$row['nom'].' '.$row['prenom'].'</OPTION>';
      }

    }
    else
      return ;
  }
  else
    return ;
}

function get_name($id_enseignant,$type)
{
  $requete = "SELECT nom, prenom FROM ".$type." WHERE
    id_".$type." = '".$id_enseignant."' ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $row = mysql_fetch_array($requete_result);

    return $row['nom'].' '.$row['prenom'];

  }
  else
    return ;
}

function get_projet($id_eleve)
{
  $requete = "SELECT mobile, description FROM projet WHERE
    id_eleve = '".$id_eleve."' ";

  $requete_result = mysql_query($requete);

  if( $requete_result != FALSE )
  {

    $nb_projet = mysql_num_rows($requete_result);
    if( $nb_projet )
    {
      $row = mysql_fetch_array($requete_result);

      return array("mobile" => $row['mobile'], "desc" => $row['description']);
    }else
      return "Aucun projet";
  }
  else
    return "Aucun projet";
}

/********************************************************
 * Traitement AJAX :                                    *
 * Affichage d'un bilan pour un élève donné             *
 * *****************************************************/

if( isset($_POST['type']) && $_POST['type'] == "eleve" ) {

  $type = $_POST['type'];
  $erreur = ""; 
  $id_eleve = $_POST['id_eleve'];
  $id_enseignant = $_SESSION['id_membre'];

  if( !isset($id_eleve) || empty($id_eleve) )
    $erreur = "<div class='erreur_div'>Erreur : Champs élève vide</div>";

  if( !isset($id_enseignant) || empty($id_enseignant))
    $erreur = "<div class='erreur_div'>Erreur : Champs enseignant vide</div>";

  if( $erreur != "" )
    echo $erreur;
  else 
  {
    echo "<div class='contenu_haut'>.: Liste des séances :.</div><div class='contenu_fond'>
      <div id='seance_view'>
      <table>
      <tr>
      <td><h3>Eleve : </h3></td>
      <td>".get_name($id_eleve,"eleve")."</td>
      </tr>
      <tr>
      <td><h3>Projet : </h3></td>
      ";
    $projet = get_projet($id_eleve);
    if(count($projet) == 1) {
      echo "<td>Aucun projet défini</td></tr></table>";
    }
    else
    {
      echo "<td>".$projet['mobile']."</td>
        </tr>
        <tr>
        <td><h3>Description du projet : </h3></td>
        <td>".$projet['desc']."</td>
        </tr>
        </table>
        ";
    }
    echo "        
      </div>
      <div id='table_bilan'>
      <table>
      <tr>
      <th>Date de la séance</td><th>Plus de détails</td>
      </tr>

      ";

    get_seance($id_eleve);          

    echo "          
      </table>
      </div>
      <div id='seance_view'>

      <input type='button' name='retour' value='Retour' id='change' onClick='javascript:retour_bilan_eleve(1)'/>
      </div>
      </div><div class='contenu_bas'></div>

      <div id='detail_seance'></div>
      ";

  }
}

/********************************************************
 * Traitement AJAX :                                    *
 * Affichage des détails pour un bilan de seance        *
 * *****************************************************/

if( isset($_POST['type']) && $_POST['type'] == "bilan" )
{
  $type = $_POST['type'];
  $erreur = "";
  $id_seance = $_POST['id_seance'];

  if( !isset($id_seance) || empty($id_seance) )
    $erreur = "<div class='erreur_div'>Erreur : Champs séance vide</div>";

  if( $erreur != "" )
    echo $erreur;
  else{

    echo "<div class='contenu_haut'>.: Détail de la séance :.</div><div class='contenu_fond'>
      <div id='table_bilan'>
      <div id='seance_view'><h3>Séance prévue</h3></div>
      <table>
      <tr>
      <th>Nom de l'atelier</td><th>Charge</td><th>Série</td><th>Répétition</td>
      </tr>
      ";
    get_detail_seance($id_seance);

    echo "    
      </table>";

    $bilan = bilan_seance($id_seance);

    echo " <div id='seance_view'><h3>Bilan de séance</h3></div>
      <table>
      <tr>
      <th>Ressenti</td>
      <th>Commentaire</th>
      </tr>
      <tr>
      ";
      
    if(count($bilan) <= 1) {
      echo "<td>-</td><td>-</td>";
    }else{
      echo "    <td>".$bilan['ressenti']."</td>
        <td>".$bilan['commentaire']."</td>
        ";
    }
    echo " 
      </tr>
      </table>";

    echo " <table>
      <tr>
      <th>Nom de l'atelier</td><th>Charge</td><th>Série</td><th>Répétition</td>
      </tr>
      ";
    get_detail_seance_bilan($id_seance);
    echo " </table></div></div><div class='contenu_bas'></div>";

  }
}

/********************************************************
 * Traitement AJAX :                                    *
 * Affichage des séances pour une date donnée           *
 * *****************************************************/

if( isset($_POST['type']) && $_POST['type'] == "date" )
{
  $type = $_POST['type'];
  $erreur = "";
  $date = $_POST['date'];

  if( !isset($date) || empty($date) )
    $erreur = "<div class='erreur_div'>Erreur : Champs date vide</div>";

  if( $erreur != "" )
    echo $erreur;
  else {
    echo "<div class='contenu_haut'>.: Liste des séances :.</div><div class='contenu_fond'>
      <div id='table_bilan_date'>
      <table>
      <tr>
      <th>Eleve</td><th>Projet</th><th>Desciption projet</th><th>Plus de détails</th>
      </tr>";

    get_seance_par_date($date);

    echo "
      </table>
      </div>
      <div id='seance_view'>
      <input type='button' name='retour' value='Retour' id='change' onClick='javascript:retour_bilan_eleve(2)'/>
      </div>
      </div>
      <div class='contenu_bas'></div>
      <div id='detail_seance'></div>
      ";
  }
}

?>
